<?php

namespace App\Models;

class Permission extends \Spatie\Permission\Models\Permission
{
    protected $table = 'permissions';
    //public $timestamps = false;

    const isMenuTrue = 1;
    const isMenuFalse = 0;

    const statusTrue = 1;
    const statusFalse = 0;

    public static function getPermissionFormSelect($parent_id=0,$selected_id = false){
        $data = self::where('parentid' , $parent_id)
            ->where('ismenu',self::isMenuTrue)
            ->select('id','parentid','name')
            ->orderBy('weight' ,'desc')
            ->get();
        $list = [];
        if (!empty($data)) {
            foreach ($data as $key => $val) {
                $list[$val->id] = [
                    'name'     => $val->name,
                    'value'    => $val->id,
                ];
                if($selected_id == $val->id){
                    $list[$val->id]['selected'] = 'selected';
                }
                if (!empty($child = self::getPermissionFormSelect($val->id,$selected_id))) {
                    $list[$val->id]['children'] = $child;
                }
            }
        }
        return array_values($list);
    }
}
